//
//  AppDelegate.swift
//  1026SwiftSinaWeiBo
//
//  Created by Apple on 15/10/26.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //测试解档
        let dict = ZDAccount.loadAccountData()
        print("dict = \(dict)");
        
        //创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //？表示 如果window有值，就执行后面的点操作，如果window没值，则不执行
        window?.rootViewController = defaultController()
        
        //设置主窗口并可见
        window?.makeKeyAndVisible()
        
        //设置全局bar
        setupAppearanceBar()
        
        return true
    }
    
    //MARK: - 显示控制器
    private func defaultController() -> UIViewController {
    
        //如果没有登录 
        if !ZDAccount.isLogin {
            
            //如果没有登录 并且是新版本(第一次下载下来打开) 显示新版本控制器; 如果没有登录不是新版本显示主控制器
            return isNewVersion() ? ZDNewFeatureViewController() : ZDMainViewController()
        }
        
        //如果已经登录 并且是新版本 就显示新版本控制器; 如果已经登录 不是新版本 就显示欢迎控制器
        return isNewVersion() ? ZDNewFeatureViewController() : ZDWelcomeViewController()
        
//        //如果已经登录 并且是新版本 就显示新版本控制器
//        if isNewVersion() {
//        
//            return ZDNewFeatureViewController()
//        }
//        
//        //如果已经登录 不是新版本 就显示欢迎控制器
//        return ZDWelcomeViewController()
    
    }
    
    //MARK: - 是否新版本
    private func isNewVersion() -> Bool {
    
        //获取当前版本
        let currentVersionStr = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        let currentVersion = Double(currentVersionStr)!
        
        print(currentVersion)
        
        //获取沙盒版本
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey("sandBoxVersionKey")
        
        print(sandBoxVersion)
        
        //保存当前版本
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: "sandBoxVersionKey")
        
        //比较两个版本 返回结果
        return currentVersion > sandBoxVersion
    }
    
    
    //MARK: - 切换控制器
    func switchController(controller: UIViewController) {
    
        window?.rootViewController = controller
    }
    
    
    //MARK: - 设置全局bar ———— 只执行一次
    private func setupAppearanceBar() {
    
        //设置导航条的主题颜色 ———— 默认是渲染成蓝色
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }

    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

